<template>
  <div class="component-distribution-prod">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="shopName"
            :label="$t('coupon.shopName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.shopName"
              type="text"
              clearable
              :placeholder="$t('coupon.shopName')"
            />
          </el-form-item>
          <el-form-item
            prop="prodName"
            :label="$t('coupon.commodityName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.prodName"
              type="text"
              clearable
              :placeholder="$t('coupon.commodityName')"
            />
          </el-form-item>
          <el-form-item
            prop="nickName"
            :label="$t('distributionProdLog.distributor') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionProdLog.distributor')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('users.phoneNumber') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              maxlength="11"
              :placeholder="$t('users.phoneNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionProdLog.distributionStatus') + ':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('distributionProdLog.distributionStatus')"
              clearable
            >
              <el-option
                :label="$t('distributionProdLog.pendingSettlement')"
                :value="1"
              />
              <el-option
                :label="$t('distributionProdLog.alreadySettled')"
                :value="2"
              />
              <el-option
                :label="$t('distributionProdLog.invalidOrder')"
                :value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('distribIncome.orderNumber') + ':'">
            <el-input
              v-model="searchForm.orderNumber"
              type="text"
              clearable
              :placeholder="$t('distribIncome.orderNumber')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con sales-record-table">
        <el-table
          ref="salesRecordTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <!-- 商城名称 -->
          <el-table-column
            prop="shopName"
            min-width="140"
            fixed
            :label="$t('coupon.shopName')"
          >
            <template #default="scope">
              {{ scope.row.shopName || '-' }}
            </template>
          </el-table-column>
          <!-- 商品信息 -->
          <el-table-column
            min-width="260"
            :label="$t('product.prodInfo')"
          >
            <template #default="{row}">
              <div class="table-cell-con">
                <div class="table-cell-image">
                  <ImgShow
                    :src="row.pic"
                    :img-alt="row.prodName"
                  />
                </div>
                <span class="table-cell-text">{{ row.prodName }}</span>
              </div>
            </template>
          </el-table-column>
          <!--佣金类型-->
          <el-table-column
            :label="$t('distributionProdLog.incomeType')"
            prop="incomeType"
            width="120"
          >
            <template #default="scope">
              <span v-if="scope.row.incomeType === 1">{{ $t('distributionProdLog.Reward1') }}</span>
              <span v-if="scope.row.incomeType === 2">{{ $t('distributionProdLog.Reward2') }}</span>
            </template>
          </el-table-column>
          <!-- 佣金数额 -->
          <el-table-column
            prop="incomeAmount"
            :label="$t('distributionProdLog.commissionAmount')"
          >
            <template #default="scope">
              {{ scope.row.incomeAmount || '-' }}
            </template>
          </el-table-column>
          <!-- 分销员 -->
          <el-table-column
            prop="nickName"
            :label="$t('distributionProdLog.distributor')"
          >
            <template #default="scope">
              {{ scope.row.nickName || '-' }}
            </template>
          </el-table-column>
          <!-- 手机号 -->
          <el-table-column
            prop="userMobile"
            :label="$t('users.phoneNumber')"
            min-width="120"
          >
            <template #default="scope">
              {{ scope.row.userMobile || '-' }}
            </template>
          </el-table-column>
          <!-- 下单时间 -->
          <el-table-column
            prop="placeTime"
            :label="$t('order.createTime')"
            min-width="180"
          >
            <template #default="scope">
              {{ scope.row.placeTime || '-' }}
            </template>
          </el-table-column>
          <!-- 订单号 -->
          <el-table-column
            prop="orderNumber"
            width="180"
            :label="$t('chat.orderNumber')"
          >
            <template #default="scope">
              {{ scope.row.orderNumber || '-' }}
            </template>
          </el-table-column>
          <!-- 分销状态 -->
          <el-table-column
            align="center"
            prop="state"
            :label="$t('distributionProdLog.distributionStatus')"
          >
            <template #default="scope">
              <span v-if="scope.row.state === -1">{{ $t('distributionProdLog.invalidOrder') }}</span>
              <span v-if="scope.row.state === 1">{{ $t('distributionProdLog.pendingSettlement') }}</span>
              <span v-if="scope.row.state === 2">{{ $t('distributionProdLog.alreadySettled') }}</span>
            </template>
          </el-table-column>
          <!--失效-->
          <el-table-column
            align="center"
            prop="reson"
            :label="$t('distributionProdLog.lapseCase')"
          >
            <template #default="scope">
              <span v-if="scope.row.reson === 0">-</span>
              <span v-if="scope.row.reson === 1">{{ $t('distributionProdLog.lapse1') }}</span>
              <span v-if="scope.row.reson === 2">{{ $t('distributionProdLog.lapse2') }}</span>
              <span v-if="scope.row.reson === 3">{{ $t('distributionProdLog.lapse3') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
const searchForm = ref({})
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/platform/distributionProd/getDistributionProdLogPage'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10
    }, tempSearchForm))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
    })
}
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
</script>
